package org.jeecg.modules.demo.jxcx.controller;



import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.jxcx.Utils.R;
import org.jeecg.modules.demo.jxcx.po.JxcCustom;
import org.jeecg.modules.demo.jxcx.po.JxcSale;
import org.jeecg.modules.demo.jxcx.service.JxcCustomService;
import org.jeecg.modules.demo.jxcx.service.JxcSaleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * (JxcSale)表控制层
 *
 * @author makejava
 * @since 2024-07-23 17:29:01
 */
@RestController
@RequestMapping("jxcSale")
public class JxcSaleController{
    private static Logger logger = LoggerFactory.getLogger(JxcSaleController.class);
    /**
     * 服务对象
     */
    @Resource
    private JxcSaleService jxcSaleService;
    @Resource
    private JxcCustomService jxcCustomService;

    @PostMapping("/queryall")
    @ResponseBody
    public R queryall(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
        return new R().ok(jxcSaleService.getByConpanyId(companyId));
    }

    @PostMapping("/likeall")
    @ResponseBody
    public R likeall(@RequestBody JSONObject params) {
        String like = params.getString("like");
        String companyId = params.getString("companyId");
        return new R().ok(jxcSaleService.getBylike(like, companyId));
    }
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param jxcSale 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<JxcSale> page, JxcSale jxcSale) {
        return new R().ok(this.jxcSaleService.page(page, new QueryWrapper<>(jxcSale)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return new R().ok(this.jxcSaleService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param jxcSale 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public R insert(@RequestBody JxcSale jxcSale) {
        this.add(jxcSale);
        return new R().ok(this.jxcSaleService.save(jxcSale));
    }
        public void add(JxcSale jxcSale){
            LocalDateTime now = LocalDateTime.now();
            // 定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            // 格式化当前的日期和时间
            String formattedDateTime = now.format(formatter);
            String id = now.format(formatter2);
            //生成6位随机整数
//        id = "1"+String.valueOf((int)(Math.random()*900000+100000))+id;
            jxcSale.setId(id);
            jxcSale.setCreateTime(formattedDateTime);
            String buyer = jxcSale.getBuyer();
            String companyId = jxcSale.getCompanyId();
            String number = jxcSale.getNumber();
            String unitPrice = jxcSale.getUnitPrice();
            //用bigdecimal计算总额，小数点位数为0
            String totalfee = String.valueOf(new BigDecimal(number).multiply(new BigDecimal(unitPrice)));
            JxcCustom jxcCustom = jxcCustomService.getBy(buyer, companyId);
            if (jxcCustom == null){
                JxcCustom jxcCustom1 = new JxcCustom();
                jxcCustom1.setId(id);
                jxcCustom1.setCreateTime(formattedDateTime);
                jxcCustom1.setName(buyer);
                jxcCustom1.setCompanyId(companyId);
                jxcCustom1.setAmount(totalfee);
                jxcCustomService.save(jxcCustom1);
            }else {
                jxcCustom.setAmount(new BigDecimal(jxcCustom.getAmount()).add(new BigDecimal(totalfee)).toString());
                jxcCustomService.updateById(jxcCustom);
            }
            jxcSale.setAmount(totalfee);
        }
    /**
     * 修改数据
     *
     * @param jxcSale 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public R update(@RequestBody JxcSale jxcSale) {
        return new R().ok(this.jxcSaleService.updateById(jxcSale));
    }

    /**
     * 删除数据
     *
     * @param
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestBody JSONObject params) {
        JSONArray idList = params.getJSONArray("idList");
        logger.info("删除数据"+idList);
        return new R().ok(this.jxcSaleService.removeByIds(idList));
    }
}

